const express = require("express")
const {
    userInfo,
    login,
    register,
    queryHomestay,
    addHomestay,
    deleteHomestay,
    modifyHomestay,
    uploadImage,
    reserveHomestay,
    checkAllOrderForm,
    cancelHomestay,
    modifyOrderForm } = require("./sqlFun")
const router = express.Router()

// 所有接口

router.get("/", (req, res) => {
    console.log(req.query)
    const { } = req.query
    res.send("234d")
})

router.post("/api", (req, res) => {
    const { } = req.body
    if (res.statusCode === 200) {
        res.send({
            rc: 1,
            data: {
                name: "小红"
            }
        })
    } else {
        res.send({
            rc: 0,
            data: {}
        })
    }
})
// 登录
router.post("/login", async (req, res) => {
    const { username, password } = req.body
    try {
        const data = await login(username, password)
        res.send({
            rc: 1,
            obj: {
                data
            }
        })
    } catch (error) {
        res.send({
            rc: 0,
            obj: {
                data: [],
                mes: "没有检索到数据"
            }
        })
    }
})
// 注册
router.post("/register", async (req, res) => {
    const { username, password } = req.body
    console.log(username, password);
    const data = await register(username, password)
    res.send({
        rc: 1,
        obj: {
            data,
            statusCode: res.statusCode,
        }
    })
})

// 用户功能

// 修改订单
router.post("/modifyOrderForm", async (req, res) => {
    const { id, username, phone, date } = req.body
    const data = await modifyOrderForm(id, username, phone, date)
    res.send({
        obj: {
            data: data
        }
    })
})

// 退订名宿
router.post("/cancelHomestay", async (req, res) => {
    const { id, username, phone } = req.body
    const data = await cancelHomestay(id, username, phone)
    res.send({
        obj: {
            data: data
        }
    })
})
// 预订民宿
router.post("/reserveHomestay", async (req, res) => {
    const { title, price, des, img, name, phone, date, username } = req.body
    const data = await reserveHomestay(title, price, des, img, name, phone, date, username)
    res.send({
        obj: {
            data: data
        }
    })
})
// 查看所有订单
router.post("/checkAllOrderForm", async (req, res) => {
    const { username } = req.body
    const data = await checkAllOrderForm(username)
    res.send({
        obj: {
            data: data
        }
    })
})


// 管理员功能

// 拿到民宿所有数据
router.get("/queryHomestay", async (req, res) => {
    const data = await queryHomestay()
    res.send({
        obj: {
            data: data
        }
    })
})
// 增加民宿
router.post("/addHomestay", async (req, res) => {
    const { title, price, des, img } = req.body
    const data = await addHomestay(title, price, des, img)
    res.send({
        obj: {
            data: data
        }
    })
})
// 修改民宿
router.post("/modifyHomestay", async (req, res) => {
    const { id, title, price, des } = req.body
    const data = await modifyHomestay(id, title, price, des)
    res.send({
        obj: {
            data: data
        }
    })
})
// 删除民宿
router.post("/deleteHomestay", async (req, res) => {
    const { id } = req.body
    const data = await deleteHomestay(id)
    res.send({
        obj: {
            data: data
        }
    })
})
// 获得上传图片的url
router.post("/uploadImage", uploadImage.single('file'), async (req, res) => {
    res.send({
        obj: {
            data: {
                data: req.file,
                msg: "图片的url"
            }
        }
    })
})


module.exports = router